package com.wondersgroup.drools.chapter3;

import com.wondersgroup.drools.BaseTest;
import com.wondersgroup.drools.model.Car;
import com.wondersgroup.drools.model.Person;
import org.junit.Test;
import org.kie.api.runtime.KieSession;

public class Drools7ApiTest extends BaseTest {

    @Test
    public void testDrools7Api() {
        // 加载META-INF目录下的kmodule.xml，且agenda-group 为 test-drools7的所有规则
        // 默认kmodule.xml位置，参见KieModuleModelImpl.java说明
        KieSession kieSession = getKieSession("test-drools7");

        Person p1 = new Person();
        p1.setAge(30);
        Car c1 = new Car();
        c1.setPerson(p1);

        Person p2 = new Person();
        p2.setAge(70);
        Car c2 = new Car();
        c2.setPerson(p2);

        // 插入工作内存
        kieSession.insert(c1);
        kieSession.insert(c2);

        // 触发规则
        int count = kieSession.fireAllRules();

        // 释放
        kieSession.dispose();

        System.out.println("Fire " + count + " rule(s)!");
        System.out.println("The discount of c1 is " + c1.getDiscount() + "%");
        System.out.println("The discount of c2 is " + c2.getDiscount() + "%");

    }

}
